﻿using DG.Tweening;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class MyTools {
    //曲线--------------------------------------------------------------------------------------------------
    public static Vector3 F_计算抛物线锚点和曲线Ease(Vector3 抛出点, Vector3 目标点, out Ease ease) {
        Vector3 mid = Vector3.Lerp(抛出点, 目标点, 0.5f);
        ease = Ease.OutSine;
        if (抛出点.y > 目标点.y) {
            mid.x = 目标点.x;
            ease = Ease.InCubic;
        } else {
            mid.y += Mathf.Abs(抛出点.y - 目标点.y);
        }
        return mid;
    }
    public static Vector3[] F_抛物线路径(Vector3 抛出点, Vector3 锚点, Vector3 目标点, int 细节 = 10) {
        Vector3[] poss = new Vector3[细节];
        for (int i = 0; i < 细节; i++) {
            poss[i] = 贝塞尔曲线(抛出点, 锚点, 目标点, (float)i / (细节 - 1));
        }
        return poss;
    }
    public static Vector3 贝塞尔曲线(Vector3 p0, Vector3 p1, Vector3 p2, float t) {
        float v = 1 - t;
        return v * v * p0 + 2 * t * v * p1 + t * t * p2;
    }
}
public static class StaticExtensions {
    /// <summary>
    /// 往list里面添加list，但是添加前会判空
    /// </summary>
    public static void TryAddRange<T>(this List<T> lists, IEnumerable<T> its) {
        if (its != null && its.Count() != 0) {
            lists.AddRange(its);
        }
    }

    /// <summary>
    /// 自身和目标点之间距离的平方
    /// </summary>
    public static float Distance_2(this Vector2 v1, Vector2 v2) {
        float x = v2.x - v1.x;
        float y = v2.y - v1.y;
        return x * x + y * y;
    }
    /// <summary>
    /// 自身和目标点之间距离的平方
    /// </summary>
    public static float Distance_2_2(this Vector2 v1, Vector2 v2) {
        return (v2 - v1).sqrMagnitude;
    }

    /// <summary>
    /// 打乱顺序
    /// </summary>
    public static void RandOrder<T>(this List<T> list) {
        int count = list.Count;
        for (int i = 0; i < count; i++) {
            int randIndex = Random.Range(0, count);
            T temp = list[i];
            list[i] = list[randIndex];
            list[randIndex] = temp;
        }
    }
}